import { defineStore } from 'pinia';
//import { getConfig } from '@/api/app';
import $H from '@/common/js/request.js';

interface AppState {
	config: Record<string, any>
}

export const useAppStore = defineStore({
	id: 'appStore',
	state: (): AppState => ({
		config: {}
	}),
	getters: {
		getTabbarConfig: (state) => uni.getStorageSync('tabbar') || [],
		getStyleConfig: (state) => uni.getStorageSync('style_tab') || {},
	},
	actions: {
		getImageUrl(url: string) {
			return url.startsWith('http') ? url : `${this.config.domain}${url}`;
		},
		async getConfig() {
			try {
				const res = await $H.post('index/config', {}, {
					token: false
				});
				if (res && res.data) {
					this.config = res.data;
					
					uni.setStorageSync('app_name', res.data.site.name);
					uni.setStorageSync('app_logo', res.data.site.logo);
					
					uni.setStorageSync('tabbar', this.config.tabbar);
					uni.setStorageSync('style_tab', this.config.style);
					
					uni.setStorageSync('oss_domain', res.data.domain);
				} else {
					console.error('无效的响应结构:', res);
				}
			} catch (error) {
				console.error('获取配置时出错:', error);
			}
		}
	}
});